<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/12/20
 * Time: 10:12
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Notice extends Base
{
    /**
     * 系统消息列表
     * @return mixed|\think\response\Json
     */
    public function systemNotice(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where['n_type'] = 1;
            //消息标题
            if(isset($params['n_title']) && $params['n_title'] != ''){
                $where['n_title'] = ['like', "%" . $params['n_title'] . "%"];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('notice')->where($where)->count();// 获取总条数
            $lists = Db::name('notice')
                ->where($where)
                ->field('n_id, n_title, n_content, n_time')
                ->page($Nowpage,$limits)
                ->order('n_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('notice/systemnotice');
    }
    
    /**
     * 发布系统消息
     * @return mixed|\think\response\Json
     */
    public function systemNoticeAdd(){
        if(Request::instance()->isPost()){
            $params = input('param.');
            $data = [
                'n_title' => $params['title'],//标题
                'n_content' => $params['n_content'],//内容
                'n_type' => 1,//系统消息
                'n_read' => ',0,',//已读
                'n_delete' => ',0,',//已删除
                'n_time' => date('Y-m-d H:i:s')//发布时间
            ];
            $res = Db::name('notice')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '发布成功']);
            }else{
                return json(['code' => 100, 'msg' => '发布失败']);
            }
        }
        return $this->fetch('notice/systemnoticeadd');
    }
    
    /**
     * 编辑系统消息
     * @return mixed|\think\response\Json
     */
    public function systemNoticeEdit(){
        if(Request::instance()->isPost()){
            $params = input('param.');
            $data = [
                'n_title' => $params['title'],//标题
                'n_content' => $params['n_content'],//内容
                'n_type' => 1,//系统消息
                'n_read' => ',0,',//已读
                'n_delete' => ',0,',//已删除
                'n_time' => date('Y-m-d H:i:s')//发布时间
            ];
            $res = Db::name('notice')
                ->where('n_id', $params['n_id'])
                ->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '修改成功']);
            }else{
                return json(['code' => 100, 'msg' => '修改失败']);
            }
        }
        $n_id = input('id');
        //要修改的系统消息
        $notictInfo = Db::table('up_notice')
            ->field('n_title, n_content')
            ->where('n_id', $n_id)
            ->find();
        $this->assign([
            'info' => $notictInfo,
            'n_id' => $n_id
        ]);
        return $this->fetch('notice/systemnoticeedit');
    }
    
    /**
     * 删除系统消息
     * @return \think\response\Json
     */
    public function systemNoticeDel(){
        $n_id = input('id');
        $res = Db::table('up_notice')->delete($n_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 交易消息列表
     * @return mixed|\think\response\Json
     */
    public function dealNotice(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where['n_type'] = 2;
            //消息标题
            if(isset($params['n_title']) && $params['n_title'] != ''){
                $where['n_title'] = ['like', "%" . $params['n_title'] . "%"];
            }
            //用户名
            if(isset($params['user_username']) && $params['user_username'] != ''){
                $where['user_username'] = ['like', "%" . $params['user_username'] . "%"];
            }
            //手机号
            if(isset($params['user_phone']) && $params['user_phone'] != ''){
                $where['user_phone'] = ['like', "%" . $params['user_phone'] . "%"];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $lists = Db::name('notice')
                ->join('up_u_user', 'user_id = n_user_id', 'left')
                ->where($where)
                ->field('n_id, n_title, n_content, n_time, user_username, user_phone')
                ->page($Nowpage,$limits)
                ->order('n_time desc')
                ->select();
            $count = count($lists);// 获取总条数
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('notice/dealnotice');
    }
}